package com.sure.supermarketserver.sell.cash.service.impl;

import com.sure.supermarketserver.sell.cash.dao.CashDao;
import com.sure.supermarketserver.sell.cash.model.pojo.Goods;
import com.sure.supermarketserver.sell.cash.model.to.CashEndPrice;
import com.sure.supermarketserver.sell.cash.model.to.CashOrder;
import com.sure.supermarketserver.sell.cash.model.to.CashOrderClient;
import com.sure.supermarketserver.sell.cash.model.to.CashOrderInfo;
import com.sure.supermarketserver.sell.cash.model.vo.CashCalcVo;
import com.sure.supermarketserver.sell.cash.model.vo.CashClientVo;
import com.sure.supermarketserver.sell.cash.service.CashService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Lei
 * @create 2021-07-21 13:09
 */
@Service
@Transactional
public class CashServiceImpl implements CashService {

    @Resource
    private CashDao cashDao;


    @Override
<<<<<<< HEAD
    public Object calcByGoId(String or_id,String id,String st_id) {
=======
    public Object calcByGoId(String or_id,String id) {
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
        Goods aGoodsById = cashDao.getAGoodsById(id);
        if (aGoodsById != null) {           //如果有这个货物，我才进行之后的操作，不然就灭有了
            CashOrderInfo orderInfoByOrId = cashDao.getOrderInfoByOrId(or_id);
            if (orderInfoByOrId == null){           //由于外键，所以第一次需要添加order_information这个记录，初始的订单的总值为零
                CashOrderInfo cashOrderInfo = new CashOrderInfo();
                cashOrderInfo.setOr_id(or_id);
                cashOrderInfo.setOr_sum(0);
<<<<<<< HEAD
                cashOrderInfo.setSt_id(st_id);
=======
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
                cashDao.insertIntoOrderInfo(cashOrderInfo);
            }

            float price = aGoodsById.getGo_price() * aGoodsById.getGo_discount() / 10;      //进行结算
            int orderDetailCount = cashDao.getOrderDetailCount();
            CashOrder cashOrder = new CashOrder();
            cashOrder.setOd_price(price);
            cashOrder.setGo_id(id);
            cashOrder.setOd_id(String.format("%03d", orderDetailCount + 1));        //001\002\003
            cashOrder.setOd_number(1);
            cashOrder.setBr_id(aGoodsById.getBr_id());
            cashOrder.setOr_id(or_id);

            cashDao.insertIntoOrderDetail(cashOrder);           //添加一个order_detail
<<<<<<< HEAD
            cashDao.delgoods(cashOrder);
=======
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
            String ca_id = aGoodsById.getCa_id();
            String caName = cashDao.getCaName(ca_id);
            CashCalcVo cashCalcVo = new CashCalcVo();           //返回的一个表格的一条记录
            cashCalcVo.setOr_id(or_id);
            cashCalcVo.setOd_id(String.format("%03d", orderDetailCount + 1));
            cashCalcVo.setCa_name(caName);
            cashCalcVo.setOd_price(price + "");
            return cashCalcVo;
        }
        return null;
    }

    /**
     * @Author Lei
     * @Description 获取到最新的or_id
     * @Date 20:13 2021/7/21
     * @Param []
     * @return java.lang.String
     **/
    @Override
    public String getOrId() {
        int orderInformationCount = cashDao.getOrderInformationCount();
        String or_id = String.format("%04d", orderInformationCount + 1);
        return or_id;
    }

    @Override
    public int updateLastPrice(CashEndPrice cashEndPrice) {
        int res = cashDao.updateOrderInfoEndPrice(cashEndPrice);
        return res;
    }

    @Override
    public int addScore(CashOrderClient cashOrderClient) {
        String cl_id = cashOrderClient.getCl_id();
        CashClientVo byClId = cashDao.getByClId(cl_id);
        cashOrderClient.setSum(cashOrderClient.getSum()+byClId.getCl_credit());
        int res = cashDao.addScore(cashOrderClient);
        return res;
    }
}
